package com.example.his.api.front.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.example.his.api.common.R;
import com.example.his.api.config.sa_token.StpCustomerUtil;
import com.example.his.api.front.controller.form.SendSmsCodeForm;
import com.example.his.api.front.controller.form.customer.LoginForm;
import com.example.his.api.front.controller.form.customer.UpdateCustomerForm;
import com.example.his.api.front.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController("FrontCustomerController")
@RequestMapping("/front/customer")
public class CustomerController {
  @Resource
  private CustomerService customerService;
  @Autowired
  private StpCustomerUtil stpCustomerUtil;
  
  
  @PostMapping("/sendSmsCode")
  public R sendSmsCode(@RequestBody @Valid SendSmsCodeForm form) {
    //    Boolean bool = customerService.sendSmsCode(form.getTel());
    //    String msg = bool ? "短信验证码已发送" : "无法发送验证码";
    //    return R.ok(msg).put("result", bool);
    HashMap res = customerService.sendSmsCode(form.getTel());
    Boolean isSend = MapUtil.getBool(res, "isSend");
    String msg = isSend ? "短信验证码已发送" : "无法发送验证码";
    return R.ok(msg).put("result", res);
  }
  
  @PostMapping("/login")
  public R login(@RequestBody @Valid LoginForm form) {
    HashMap map = customerService.login(form.getTel(), form.getCode());
    Boolean result = MapUtil.getBool(map, "result");
    String msg = MapUtil.getStr(map, "msg");
    R r = R.ok(msg).put("result", result);
    
    if(result) {
      //      生成令牌
      int id = MapUtil.getInt(map, "id");
      StpCustomerUtil.login(id, "PC");
      String token = StpCustomerUtil.getTokenValue();
      r.put("token", token);
    }
    return r;
  }
  
  @GetMapping("/logout")
  @SaCheckLogin(type = StpCustomerUtil.TYPE)
  public R logout() {
    int loginId = StpCustomerUtil.getLoginIdAsInt();
    StpCustomerUtil.logout(loginId, "PC");
    return R.ok();
  }
  
  @GetMapping("/checkLogin")
  public R checkLogin() {
    boolean bool = StpCustomerUtil.isLogin();
    return R.ok().put("result", bool);
  }
  
  @PostMapping("/searchSummary")
  @SaCheckLogin(type = StpCustomerUtil.TYPE)
  public R searchSummary() {
    int loginIdAsInt = StpCustomerUtil.getLoginIdAsInt();
    HashMap map = customerService.searchSummary(loginIdAsInt);
    
    return R.ok().put("result", map);
  }
  
  @PostMapping("/update")
  @SaCheckLogin(type = StpCustomerUtil.TYPE)
  public R update(@RequestBody @Valid UpdateCustomerForm form) {
    Map map = BeanUtil.beanToMap(form);
    int loginIdAsInt = StpCustomerUtil.getLoginIdAsInt();
    map.put("id", loginIdAsInt);
    boolean b = customerService.update(map);
    
    return R.ok().put("result", b);
  }
  
}
